package com.liu.utils;

import com.liu.Main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * @author Mr.Liu
 * @version 1.0
 * @date 2024/8/18 11:06
 */
public class CommonUtils {

    public static Properties loadProperties(String fileName) {
        Properties props = new Properties();
        InputStream inputStream = Main.class.getClassLoader().getResourceAsStream(fileName);

        if (inputStream != null) {
            try {
                props.load(inputStream);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.err.println("Properties file not found: " + fileName);
        }

        return props;
    }

    //读取sql 到内存
    public static String readSqlFile(String fileName) {
        if(fileName == null || fileName.equals("")){
            new RuntimeException("file is not found!");
        }
        StringBuilder sqlContent = new StringBuilder();
        try (InputStream inputStream = Main.class.getClassLoader().getResourceAsStream(fileName);
             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream ,"UTF-8"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                sqlContent.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sqlContent.toString();
    }


}
